package com.sc.syllabus.work_hours;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sc.syllabus.R;
import com.sc.syllabus.work_hours.bean.WorkHoursBean;
import com.srain.utils.StringUtils;

import java.util.List;

/**
 * Created by liushuanglong on 2017/9/12.
 */

public class HoursAdapter extends BaseAdapter {

    private Context mContext;
    private List<WorkHoursBean> data;
    private LayoutInflater mInflater;

    public HoursAdapter(Context context, List<WorkHoursBean> data) {
        mContext = context;
        this.data = data;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return ((null != data) ? data.size() : 0);
    }

    @Override
    public WorkHoursBean getItem(int position) {
        WorkHoursBean bean = null;
        if (null != data) {
            bean = data.get(position);
        }
        return bean;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_hours, null);
            holder.txtStart = (TextView) convertView.findViewById(R.id.text_startAt);
            holder.txtEnd = (TextView) convertView.findViewById(R.id.text_endAt);
            holder.txtAddr = (TextView) convertView.findViewById(R.id.text_address);
            holder.txtContent = (TextView) convertView.findViewById(R.id.text_content);
            holder.txtHours = (TextView) convertView.findViewById(R.id.text_hours);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (null != data) {
            WorkHoursBean bean = data.get(position);
            String start = "开始时间 : " + StringUtils.numericToStr(String.valueOf(bean.getStartAt()));
            String end = "结束时间 : " + StringUtils.numericToStr(String.valueOf(bean.getEndAt()));
            String addr = "工作地点 : " + ((0 == bean.getWorkAddress()) ? "耐拓" : "家");
            String content = "工作内容 : " + bean.getWorkContent();
            String dis = StringUtils.getDistanceTime1(bean.getStartAt(), bean.getEndAt());
            holder.txtStart.setText(start);
            holder.txtEnd.setText(end);
            holder.txtAddr.setText(addr);
            holder.txtContent.setText(content);
            holder.txtHours.setText(dis);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView txtStart;
        TextView txtEnd;
        TextView txtAddr;
        TextView txtContent;
        TextView txtHours;
    }
}
